#include <bits/stdc++.h>
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head == NULL || head->next==NULL) return head;
        ListNode *p = head;
        ListNode *ap = p->next;
        ListNode *nap = ap;
        while(ap != NULL){
            nap = nap->next;
            ap->next = p;
            p = ap;
            ap = nap;
        }
        head->next = NULL;
        head = p;
        return head;
    }
};